//
//  MenuLayer.m
//  CarDemo
//
//  Created by huang kun on 8/9/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "MenuLayer.h"
#import "ChooseCarLayer.h"
#import "OptionLayer.h"

@implementation MenuLayer

- (void) dealloc {
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}


- (id) init {
	if( (self = [super init]) ){
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		CCSprite *bg = [CCSprite spriteWithFile:@"menuBg.png"];
		[self addChild:bg];
		[bg setPosition:ccp(winSize.width/2, winSize.height/2)];
		
		
		CCMenuItemImage *startButton = [CCMenuItemImage itemFromNormalImage:@"start01.png" selectedImage:@"start03.png" 
															   target:self selector:@selector(startGame:)];
		CCMenuItemImage *continueButton = [CCMenuItemImage itemFromNormalImage:@"continue01.png" selectedImage:@"continue03.png"
																		target:self selector:@selector(continueGame:)];
		CCMenuItemImage *optionButton = [CCMenuItemImage itemFromNormalImage:@"options01.png" selectedImage:@"options03.png"
																	  target:self selector:@selector(optionScene:)];
		CCMenuItemImage *fabiaButton = [CCMenuItemImage itemFromNormalImage:@"fabiasite01.png" selectedImage:@"fabiasite03.png"
																	 target:self selector:@selector(fabiaScene:)];
		CCMenuItemImage *mailButton = [CCMenuItemImage itemFromNormalImage:@"mail01.png" selectedImage:@"mail03.png"
																	 target:self selector:@selector(sendMail:)];
		
		CCMenu *menu = [CCMenu menuWithItems:startButton,continueButton,optionButton,fabiaButton,mailButton,nil];
		[self addChild:menu z:1];
		[menu setPosition:CGPointZero];
		
		[startButton setPosition:ccp(386, 215)];
		[continueButton setPosition:ccp(386, 171)];
		[optionButton setPosition:ccp(386, 129)];
		[fabiaButton setPosition:ccp(386, 87)];
		[mailButton setPosition:ccp(33, 32)];
	}
	return self;
}

- (void) startGame:(id)sender {
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[ChooseCarLayer node]]];
}

- (void) continueGame:(id)sender {
}

- (void) optionScene:(id)sender {
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[OptionLayer node]]];
}

- (void) fabiaScene:(id)sender
{
	
	NSURL *url = [NSURL URLWithString:@"http://www.skoda.com.cn/skoda/pages/product/fabia.jsp"];
	[[UIApplication sharedApplication] openURL:url];
}

- (void) sendMail:(id)sender
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	[picker setSubject:@"For you"];
	
	[[[CCDirector sharedDirector]openGLView]addSubview:picker.view];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
	[controller.view removeFromSuperview];
}


@end
